#include <DCMotor.h>
#include <CoreLED.h>
#include <CoreKEY.h>
#include <CoreBeep.h>
#include <ExtSRAMInterface.h>
#include <LED.h>
#include <BH1750.h>
#include <Command.h>
#include <BEEP.h>
#include <Infrare.h>
#include <Ultrasonic.h>
#include <SYN7318.h>
#include <Chinese.h>
#include "Drive.h"
#include "Openmv.h"

uint8_t ZigBee_back[16] = { 0x55, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

#define TSendCycle  200 
#define ATM_Data_Length 48
uint8_t ATM_Data[ATM_Data_Length];
uint8_t ZigBee_command[8];
uint8_t ZigBee_judge;
uint8_t infrare_com[6];
uint8_t sendflag;
unsigned long frisrtime;
unsigned long Tcount;

uint8_t Data_Type;
uint8_t Data_Flag;
uint8_t Data_Length;
uint8_t Data_OTABuf[40];

//void ZigBeeRx_Handler(uint8_t *mar);
void KEY_Handler(uint8_t k_value);

void setup()
{
	CoreLED.Initialization();
	CoreKEY.Initialization();
	CoreBeep.Initialization();
	ExtSRAMInterface.Initialization();
	LED.Initialization();
	BH1750.Initialization();
	BEEP.Initialization();
	Infrare.Initialization();
	Ultrasonic.Initialization();
	DCMotor.Initialization();
  SYN7318.Initialization();
	uint8_t clear_data[8] = {0,0,0,0,0,0,0,0};
	ExtSRAMInterface.ExMem_Write_Bytes(0x6100, clear_data, 8);
	Serial.begin(115200);
	while (!Serial);

	sendflag = 0;
	frisrtime = 0;
	Tcount = 0;
}

void loop()
{
    frisrtime = millis();
  
    CoreKEY.Kwhile(KEY_Handler);      //按键检测
	Drive.Task_Out();
}


uint8_t test_buf[] = {0xFD,0x00,0x06,0x01,0x01,0xC4,0xFA,0xBA,0xC3};
/*
  语音播报任意内容
*/
void Speech_Sounds_Ctr(void)
{
   ExtSRAMInterface.ExMem_Write_Bytes(0x6180, test_buf, 13);
 }


uint8_t repo_buf[] = {0x03,0x05,0x14,0x45,0xDE,0x92};
/*
红外测试
*/
void Infrared_Test(void)
{
  Infrare.Transmition(repo_buf,6);
}

uint8_t trm_buf[] = {0xAF,0x06,0x00,0x02,0x00,0x00,0x01,0xBB};
/*
语音识别
*/
 void Speech_Disc(void)
 {
     uint8_t sph_id = 0;
     SYN7318.Start_ASR_send(4);
     delay(200);
     ExtSRAMInterface.ExMem_Write_Bytes(0x6008, Command.command33, 8);
	 delay(100);
	 ExtSRAMInterface.ExMem_Write_Bytes(0x6008, Command.command33, 8);
	 delay(100);
	 ExtSRAMInterface.ExMem_Write_Bytes(0x6008, Command.command33, 8);
	 delay(100);
     sph_id = SYN7318.Start_ASR_rec(true);
     switch(sph_id)
     {
        Serial.print(SYN7318.Start_ASR_rec(true));
        case 0x02:
          SYN7318.VSPTest(str[0],1);
          break;
        case 0x03:
          SYN7318.VSPTest(str[1],1); 
          break;
        case 0x04:
          SYN7318.VSPTest(str[2],1); 
          break;
        case 0x05:
          SYN7318.VSPTest(str[3],1); 
          break;
        case 0x06:
          SYN7318.VSPTest(str[4],1); 
          break;
        default:
          SYN7318.VSPTest(str[10],1);
          break;
      }
      trm_buf[2] = sph_id;
      ExtSRAMInterface.ExMem_Write_Bytes(0x6008, trm_buf, 8);
 }

/*
功能: 按键处理函数
参数: K_value 按键值
说明：按键4用于切换模式
      模式1： KEY1  启动全自动
	          KEY2  启动OpenMV调试模式（进入调试模式进行识别时不自动退出）
			  KEY3  退出OpenMV调试模式
      
	  模式2:  KEY1  交通灯（A）识别
	          KEY2  交通灯（B）识别
			  KEY3  自学习交通灯阈值

      模式3： KEY1  二维码识别
	          KEY2  图形识别
			  KEY3  预留
*/
void KEY_Handler(uint8_t k_value)
{
	static uint8_t key_mode = 1;
	uint8_t value[3] = { 1,2,2 };
    switch(k_value)
    {
        case 1:
          switch (key_mode)
          {
            case 1:
              Drive.Auto_Drive(Drive.AUTO_01);       //启动全自动
              break;
            case 2:
              Openmv.Traffic_Distinguish(Drive.A);   //（A）交通灯识别
              break;
            case 3:
              Serial.print("qr_value:");
              Serial.println(Openmv.QR_Distinguish(Drive.A)); //识别二维码
              break;
            case 4:
              DCMotor.Back_Black_Line();
              
              //Drive.ZH_XJ(Drive.X);               //循迹    
              break;
              }
            break;
        case 2:
              switch (key_mode)
              {
                case 1:
                  Openmv.Set_Debug_Mode();      	    //启动OpenMV调试模式
                  break;
                case 2:
                  Openmv.Traffic_Distinguish(Drive.A); //（B）交通灯识别
                  break;
                case 3:
                  Openmv.Figure_Distinguish(value);   //识别图形
                  break;
                case 4:
                  Drive.FX_Correct(1, 2, 350);               
                  break;
              }
                  break;
        case 3:
                switch (key_mode)
                {
                case 1:
                  Openmv.Res_Debug_Mode();  		//关闭OpenMV调试模式
                  break;
                case 2:
                  Openmv.Traffic_Disc_StartUp(2);  //交通灯阈值学习
                  break;
                case 3:
                  Openmv.Traffic_Disc_StartUp(2);  //交通灯阈值学习
                  //预留
                  break;
                case 4:
                  Drive.FX_Correct(2, 2, 500);
                  //Drive.FX_Correct();
                  break;
                }
                      break;
        case 4:
              key_mode++;
              if (key_mode >= 5)
              {
                key_mode = 1;
              }
              for (int i = 0; i < key_mode; i++)
              {
                BEEP.TurnOn();
                delay(100);
                BEEP.TurnOff(); 
                delay(100);
              }
                    break;
      }
  }


